package com.yc.ui.admin;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.layout.FillLayout;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import com.yc.Idao.GoodsIDao;
import com.yc.dao.GoodsDao;
import com.yc.ui.Order;
import com.yc.ui.dialog.UpdateAndAddGoodsDialog;
import com.yc.utils.MailUtils;
import com.yc.utils.SWTUtil;
import com.yc.utils.StringUtil;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.wb.swt.SWTResourceManager;

public class Good extends Composite {
	private Table table;
	private Text text_3;
	GoodsIDao dao = new GoodsDao();
	private byte[] bt;//图片的字节数组


	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public Good(Composite parent, int style) {
		super(parent, style);
		setLayout(new FillLayout(SWT.HORIZONTAL));
		
		Composite composite = new Composite(this, SWT.NONE);
		composite.setBackgroundImage( SWTResourceManager.getImage(Order.class, "/com/yc/images/客房预约1.PNG") );
		SWTUtil.imagetSize(composite);				Button btnNewButton = new Button(composite, SWT.NONE);
		btnNewButton.setLocation(469, 48);
		btnNewButton.setSize(98, 30);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				table.removeAll();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("g_name", text_3.getText().toString().trim());
				showinfo(map);
			}
		});
		btnNewButton.setText("查询");
		
		text_3 = new Text(composite, SWT.BORDER);
		text_3.setLocation(292, 50);
		text_3.setSize(117, 26);
		
		
		Label lblNewLabel_3 = new Label(composite, SWT.NONE);
		lblNewLabel_3.setLocation(111, 48);
		lblNewLabel_3.setSize(117, 26);
		lblNewLabel_3.setText("按商品名称查询：");
		
		table = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		table.setLocation(48, 127);
		table.setSize(961, 429);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				UpdateAndAddGoodsDialog up = new UpdateAndAddGoodsDialog(getShell(), SWT.NONE);
				up.open(table);
				showinfo(null);
			}
		});
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(239);
		tblclmnNewColumn.setText("商品编号");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(282);
		tblclmnNewColumn_2.setText("商品名称");
		
		TableColumn tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(250);
		tblclmnNewColumn_3.setText("价格");
		
		TableColumn tblclmnNewColumn_4 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_4.setWidth(182);
		tblclmnNewColumn_4.setText("商品图片");
		
		Button btnNewButton_1 = new Button(composite, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				UpdateAndAddGoodsDialog up = new UpdateAndAddGoodsDialog(getShell(), SWT.NONE);
				up.open();
				showinfo(null);
			}
		});
		btnNewButton_1.setBounds(702, 48, 98, 30);
		btnNewButton_1.setText("添加商品");

		showinfo(null);
		

		showinfo(null);
	}

	private void showinfo(Map<String, Object> map) {
		//查询信息‘
		List<Map<String, Object>> list = dao.find(map);
		TableItem tableItem = null;
		//判空
		if(null == list || list.isEmpty()) {
			SWTUtil.showMessage(getShell(), "查无信息");
			return;
		}
		//有信息  循环
		for(Map<String, Object> m : list) {
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(new String[] {
				StringUtil.toString(m.get("g_id")),
				StringUtil.toString(m.get("g_name")),
				StringUtil.toString(m.get("g_price"))
			});
			bt = ( byte[] )m.get("g_image");
			// 字节数组判空
			if( null != bt) {
				Image image = SWTUtil.imageSize(100, 50, new ByteArrayInputStream(bt));
				tableItem.setImage(3, image);
			}
		}
	}
	
	
}
